﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ATMAaronKnoll
{
    class ATM
    {
        static private int billCount = 500;
        static private bool userAuthenticated = false;

        static void Main(string[] args)
        {
            bool runApp = true;
            int accountInput;
            string input;

            while (runApp)
            {
                Console.Clear();
                Console.WriteLine("\n\nWelcome! Please follow the directions.");
                Console.Write("\nPlease enter your 7 digit account number: ");
                input = Console.ReadLine();
                if (input != "")
                {
                    accountInput = Convert.ToInt32(input);
                }
                else
                {
                    accountInput = 1;
                }
                Console.Write("\n\nPlease enter your PIN: ");
                input = Console.ReadLine();
                if ((accountInput != 1) && (input != null))
                {
                    if (IsAdmin(accountInput, input))
                    {
                        AdminMenu();
                    }
                    else if (Accounts.AuthenticateUser(accountInput, input))
                    {
                        userAuthenticated = true;
                    }
                    else
                    {
                        Console.Clear();
                        Console.Write("\n\nInvalid Account Number or PIN. Returning to account authentication...");
                        System.Threading.Thread.Sleep(2000);
                        Console.Clear();
                        break;
                    }
                }

                while (userAuthenticated)
                {
                    Console.Clear();
                    Console.WriteLine("\nWelcome, what would you like to do today?");
                    Console.WriteLine("\n\t1 - View My Balance");
                    Console.WriteLine("\t2 - Withdraw Cash");
                    Console.WriteLine("\t3 - Deposit Cash");
                    Console.WriteLine("\t4 - Exit");
                    Console.Write("\nPlease enter an option number or press ESC to quit: ");
                    ConsoleKeyInfo keyPressed = Console.ReadKey();
                    Console.Clear();
                    if (keyPressed.Key == ConsoleKey.Escape)
                    {
                        userAuthenticated = false;
                        Accounts.Logoff();
                        Console.WriteLine("Cancelling transaction. Please wait...");
                        System.Threading.Thread.Sleep(2000);
                        Console.Clear();
                    }
                    else
                    {
                        switch (keyPressed.Key)
                        {
                            case ConsoleKey.NumPad1:
                            case ConsoleKey.D1:
                                Console.WriteLine("Your account balance is : $" + Accounts.GetBalance());
                                Console.Write("\nNow returning to the main menu...");
                                System.Threading.Thread.Sleep(3000);
                                Console.Clear();
                                break;

                            case ConsoleKey.NumPad2:
                            case ConsoleKey.D2:
                                Withdrawals getWdMenu = new Withdrawals();
                                getWdMenu.DisplayWithdrawalMenu("");
                                break;

                            case ConsoleKey.NumPad3:
                            case ConsoleKey.D3:
                                Deposits getDepMenu = new Deposits();
                                getDepMenu.DisplayDepositMenu("");
                                break;

                            case ConsoleKey.NumPad4:
                            case ConsoleKey.D4:
                                userAuthenticated = false;
                                Accounts.Logoff();
                                Console.WriteLine("Cancelling transaction. Please wait...");
                                System.Threading.Thread.Sleep(2000);
                                Console.Clear();
                                break;
                        }
                    }
                }
            }
            Console.Write("\nExiting ATM. Press any key to close window.");
            Console.ReadKey();
        }
        public static bool IsAdmin(int account, String pin)
        {
            if ((account == 3334444) && (pin == "adm1"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static void AdminMenu()
        {
            bool done = false;
            String name, address, state, zip, pin, input;
            int accountNumber, balance;
            while (!done)
            {
                Console.Clear();
                Console.WriteLine("\nAdmin Menu");
                Console.WriteLine("\n\t1 - Add Account");
                Console.WriteLine("\t2 - Reset Machine");
                Console.WriteLine("\t3 - Shutdown Machine");
                Console.WriteLine("\t4 - Account Authentication");
                Console.Write("\nPlease enter an option number or press ESC to exit: ");
                ConsoleKeyInfo keyPressed = Console.ReadKey();

                switch (keyPressed.Key)
                {
                    case ConsoleKey.NumPad1:
                    case ConsoleKey.D1:
                        Console.Clear();
                        Console.Write("\n\nPlease enter name: ");
                        name = Console.ReadLine();
                        Console.Write("\n\nPlease enter address: ");
                        address = Console.ReadLine();
                        Console.Write("\n\nPlease enter state: ");
                        state = Console.ReadLine();
                        Console.Write("\n\nPlease enter zip: ");
                        zip = Console.ReadLine();
                        Console.Write("\n\nPlease enter account number: ");
                        input = Console.ReadLine();
                        accountNumber = Convert.ToInt32(input);
                        Console.Write("\n\nPlease enter pin: ");
                        pin = Console.ReadLine();
                        Console.Write("\n\nPlease enter account balance: ");
                        input = Console.ReadLine();
                        balance = Convert.ToInt32(input);
                        Accounts.CreateAccount(name, address, state, zip, accountNumber, pin, balance);
                        break;
                    case ConsoleKey.NumPad2:
                    case ConsoleKey.D2:
                        billCount = 500;
                        done = true;
                        break;
                    case ConsoleKey.NumPad3:
                    case ConsoleKey.D3:
                        Accounts.Logoff();
                        Environment.Exit(0);
                        break;
                    default:
                        done = true;
                        break;
                }
            }
        }

        public static bool CheckBills(int dispense)
        {
            if (billCount >= dispense)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static void DispenseBills(int dispense)
        {
            billCount = billCount - dispense;
        }

        public static void InsertBills(int take)
        {
            billCount = billCount + take;
        }
    }
}
